################################################################################
#
# uboot
#
################################################################################

THINGINO_UBOOT_VERSION = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_VERSION))
THINGINO_UBOOT_BOARD_NAME = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_BOARDNAME))

THINGINO_UBOOT_LICENSE = GPL-2.0+
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_LATEST_VERSION),y)
THINGINO_UBOOT_LICENSE_FILES = Licenses/gpl-2.0.txt
endif
THINGINO_UBOOT_CPE_ID_VENDOR = denx
THINGINO_UBOOT_CPE_ID_PRODUCT = u-boot

THINGINO_UBOOT_INSTALL_IMAGES = YES

TARGET_LDFLAGS:=$(filter-out -ztext,$(TARGET_LDFLAGS))

# u-boot 2020.01+ needs make 4.0+
THINGINO_UBOOT_DEPENDENCIES = host-pkgconf $(BR2_MAKE_HOST_DEPENDENCY)
THINGINO_UBOOT_MAKE = $(BR2_MAKE)

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL),y)
# Handle custom U-Boot tarballs as specified by the configuration
THINGINO_UBOOT_TARBALL = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL_LOCATION))
THINGINO_UBOOT_SITE = $(patsubst %/,%,$(dir $(THINGINO_UBOOT_TARBALL)))
THINGINO_UBOOT_SOURCE = $(notdir $(THINGINO_UBOOT_TARBALL))
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_GIT),y)
THINGINO_UBOOT_SITE = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_URL))
THINGINO_UBOOT_SITE_METHOD = git
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_HG),y)
THINGINO_UBOOT_SITE = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_URL))
THINGINO_UBOOT_SITE_METHOD = hg
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_SVN),y)
THINGINO_UBOOT_SITE = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_URL))
THINGINO_UBOOT_SITE_METHOD = svn
else
# Handle stable official U-Boot versions
THINGINO_UBOOT_SITE = https://ftp.denx.de/pub/u-boot
THINGINO_UBOOT_SOURCE = u-boot-$(THINGINO_UBOOT_VERSION).tar.bz2
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT)$(BR2_PACKAGE_THINGINO_UBOOT_LATEST_VERSION),y)
BR_NO_CHECK_HASH_FOR += $(THINGINO_UBOOT_SOURCE)
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_BIN),y)
THINGINO_UBOOT_BINS += u-boot.bin
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_DTB),y)
THINGINO_UBOOT_BINS += u-boot.dtb
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_ELF),y)
THINGINO_UBOOT_BINS += u-boot
# To make elf usable for debugging on ARC use special target
ifeq ($(BR2_arc),y)
THINGINO_UBOOT_MAKE_TARGET += mdbtrick
endif
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_REMAKE_ELF),y)
THINGINO_UBOOT_BINS += u-boot.elf
endif

# Call 'make all' unconditionally
THINGINO_UBOOT_MAKE_TARGET += all

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_KWB),y)
THINGINO_UBOOT_BINS += u-boot.kwb
THINGINO_UBOOT_MAKE_TARGET += u-boot.kwb
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_AIS),y)
THINGINO_UBOOT_BINS += u-boot.ais
THINGINO_UBOOT_MAKE_TARGET += u-boot.ais
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND_BIN),y)
THINGINO_UBOOT_BINS += u-boot-nand.bin
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_DTB_IMG),y)
THINGINO_UBOOT_BINS += u-boot-dtb.img
THINGINO_UBOOT_MAKE_TARGET += u-boot-dtb.img
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_DTB_IMX),y)
THINGINO_UBOOT_BINS += u-boot-dtb.imx
THINGINO_UBOOT_MAKE_TARGET += u-boot-dtb.imx
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_DTB_BIN),y)
THINGINO_UBOOT_BINS += u-boot-dtb.bin
THINGINO_UBOOT_MAKE_TARGET += u-boot-dtb.bin
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_IMG),y)
THINGINO_UBOOT_BINS += u-boot.img
THINGINO_UBOOT_MAKE_TARGET += u-boot.img
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_ITB),y)
THINGINO_UBOOT_BINS += u-boot.itb
ifneq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_BINMAN),y)
THINGINO_UBOOT_MAKE_TARGET += u-boot.itb
endif
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_QSPI_BIN),y)
THINGINO_UBOOT_BINS += qspi.bin
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_IMX),y)
THINGINO_UBOOT_BINS += u-boot.imx
THINGINO_UBOOT_MAKE_TARGET += u-boot.imx
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_SB),y)
THINGINO_UBOOT_BINS += u-boot.sb
THINGINO_UBOOT_MAKE_TARGET += u-boot.sb
# mxsimage needs OpenSSL
THINGINO_UBOOT_DEPENDENCIES += host-elftosb host-openssl
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_SD),y)
# BootStream (.sb) is generated by U-Boot, we convert it to SD format
THINGINO_UBOOT_BINS += u-boot.sd
THINGINO_UBOOT_MAKE_TARGET += u-boot.sb
THINGINO_UBOOT_DEPENDENCIES += host-elftosb host-openssl
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND),y)
THINGINO_UBOOT_BINS += u-boot.nand
THINGINO_UBOOT_MAKE_TARGET += u-boot.sb
THINGINO_UBOOT_DEPENDENCIES += host-elftosb host-openssl
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_STM32),y)
THINGINO_UBOOT_BINS += u-boot.stm32
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_FORMAT_STM32_LEGACY),y)
THINGINO_UBOOT_MAKE_TARGET += u-boot.stm32
endif
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_INITIAL_ENV),y)
THINGINO_UBOOT_MAKE_TARGET += u-boot-initial-env
define THINGINO_UBOOT_INSTALL_THINGINO_UBOOT_INITIAL_ENV
	$(INSTALL) -D -m 0644 $(@D)/u-boot-initial-env $(TARGET_DIR)/etc/u-boot-initial-env
endef
THINGINO_UBOOT_POST_INSTALL_TARGET_HOOKS += THINGINO_UBOOT_INSTALL_THINGINO_UBOOT_INITIAL_ENV
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_CUSTOM),y)
THINGINO_UBOOT_BINS += $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_CUSTOM_NAME))
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT),y)
THINGINO_UBOOT_BINS += u-boot.bin
THINGINO_UBOOT_BIN_IFT = u-boot.bin.ift
endif

# The kernel calls AArch64 'arm64', but U-Boot calls it just 'arm', so
# we have to special case it. Similar for i386/x86_64 -> x86
ifeq ($(NORMALIZED_ARCH),arm64)
THINGINO_UBOOT_ARCH = arm
else ifneq ($(filter $(NORMALIZED_ARCH),i386 x86_64),)
THINGINO_UBOOT_ARCH = x86
else
THINGINO_UBOOT_ARCH = $(NORMALIZED_ARCH)
endif

THINGINO_UBOOT_MAKE_OPTS += \
	CROSS_COMPILE="$(TARGET_CROSS)" \
	ARCH=$(THINGINO_UBOOT_ARCH) \
	HOSTCC="$(HOSTCC) $(subst -I/,-isystem /,$(subst -I /,-isystem /,$(HOST_CFLAGS)))" \
	HOSTLDFLAGS="$(HOST_LDFLAGS)" \
	$(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_MAKEOPTS))

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ENABLE_BAR),y)
THINGINO_UBOOT_MAKE_OPTS += CONFIG_SPI_FLASH_BAR=1
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_EXTERNAL_ENV_ENABLE),y)
THINGINO_UBOOT_MAKE_OPTS += CONFIG_BOOTARGS_EXTERNAL=1
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_PHY_RESET_AFTER_CONFIG),y)
THINGINO_UBOOT_MAKE_OPTS += CONFIG_PHY_RESET_AFTER_CONFIG=1
THINGINO_UBOOT_MAKE_OPTS += CONFIG_GPIO_PHY_RESET=$(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_GPIO_PHY_RESET))
THINGINO_UBOOT_MAKE_OPTS += CONFIG_GPIO_PHY_RESET_ENLEVEL=$(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_GPIO_PHY_RESET_ENLEVEL))
endif

# Disable FDPIC if enabled by default in toolchain
ifeq ($(BR2_BINFMT_FDPIC),y)
THINGINO_UBOOT_MAKE_OPTS += KCFLAGS=-mno-fdpic
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_ATF_BL31),y)
THINGINO_UBOOT_DEPENDENCIES += arm-trusted-firmware
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_ATF_BL31_ELF),y)
THINGINO_UBOOT_MAKE_OPTS += BL31=$(BINARIES_DIR)/bl31.elf
define THINGINO_UBOOT_COPY_ATF_FIRMWARE
	cp $(BINARIES_DIR)/bl31.elf $(@D)/
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_UBOOT_COPY_ATF_FIRMWARE
else
THINGINO_UBOOT_MAKE_OPTS += BL31=$(BINARIES_DIR)/bl31.bin
define THINGINO_UBOOT_COPY_ATF_FIRMWARE
	cp $(BINARIES_DIR)/bl31.bin $(@D)/
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_UBOOT_COPY_ATF_FIRMWARE
endif
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_OPTEE_TEE),y)
THINGINO_UBOOT_DEPENDENCIES += optee-os
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_OPTEE_TEE_ELF),y)
THINGINO_UBOOT_MAKE_OPTS += TEE=$(BINARIES_DIR)/tee.elf
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_OPTEE_TEE_BIN),y)
THINGINO_UBOOT_MAKE_OPTS += TEE=$(BINARIES_DIR)/tee.bin
endif
endif

# TI K3 devices needs at least ti-sysfw (System Firmware) provided
# by ti-k3-boot-firmware when built with u-boot's binman tool.
# Some TI K3 devices using a split firmware boot flow (AM62,
# j721e) also need the Device Manager (DM) firmware.
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_TI_K3_BOOT_FIRMWARE),y)
THINGINO_UBOOT_DEPENDENCIES += ti-k3-boot-firmware
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_OPENSBI),y)
THINGINO_UBOOT_DEPENDENCIES += opensbi
THINGINO_UBOOT_MAKE_OPTS += OPENSBI=$(BINARIES_DIR)/fw_dynamic.bin
endif

# Mainline U-Boot versions can create the i.MX specific boot images
# and need some NXP firmware blobs.
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_IMX_FIRMWARE),y)
THINGINO_UBOOT_DEPENDENCIES += firmware-imx
THINGINO_UBOOT_IMX_FW_FILES = \
	$(if $(BR2_PACKAGE_FIRMWARE_IMX_NEEDS_HDMI_FW),signed_hdmi_imx8m.bin) \
	$(if $(BR2_PACKAGE_FIRMWARE_IMX_LPDDR4),lpddr4*.bin) \
	$(if $(BR2_PACKAGE_FIRMWARE_IMX_DDR4),ddr4*.bin) \
	$(if $(BR2_PACKAGE_FIRMWARE_IMX_DDR3),ddr3*.bin)

define THINGINO_UBOOT_COPY_IMX_FW_FILES
	$(foreach fw,$(THINGINO_UBOOT_IMX_FW_FILES),\
		cp $(BINARIES_DIR)/$(fw) $(@D)/
	)
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_UBOOT_COPY_IMX_FW_FILES
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_ROCKCHIP_RKBIN),y)
THINGINO_UBOOT_DEPENDENCIES += rockchip-rkbin
define THINGINO_UBOOT_INSTALL_THINGINO_UBOOT_ROCKCHIP_BIN
	$(INSTALL) -D -m 0644 $(@D)/u-boot-rockchip.bin $(BINARIES_DIR)/u-boot-rockchip.bin
endef
THINGINO_UBOOT_POST_INSTALL_IMAGES_HOOKS += THINGINO_UBOOT_INSTALL_THINGINO_UBOOT_ROCKCHIP_BIN
ifneq ($(ROCKCHIP_RKBIN_BL31_FILENAME),)
THINGINO_UBOOT_MAKE_OPTS += BL31=$(BINARIES_DIR)/$(notdir $(ROCKCHIP_RKBIN_BL31_FILENAME))
endif
ifneq ($(ROCKCHIP_RKBIN_TPL_FILENAME),)
THINGINO_UBOOT_MAKE_OPTS += ROCKCHIP_TPL=$(BINARIES_DIR)/$(notdir $(ROCKCHIP_RKBIN_TPL_FILENAME))
endif
ifneq ($(ROCKCHIP_RKBIN_TEE_FILENAME),)
THINGINO_UBOOT_MAKE_OPTS += TEE=$(BINARIES_DIR)/$(notdir $(ROCKCHIP_RKBIN_TEE_FILENAME))
endif
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_DTC),y)
THINGINO_UBOOT_DEPENDENCIES += host-dtc
THINGINO_UBOOT_MAKE_OPTS += DTC=$(HOST_DIR)/bin/dtc
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_PYTHON3),y)
THINGINO_UBOOT_DEPENDENCIES += host-python3 host-python-setuptools
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_PYLIBFDT),y)
THINGINO_UBOOT_DEPENDENCIES += host-python-pylibfdt
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_PYELFTOOLS),y)
THINGINO_UBOOT_DEPENDENCIES += host-python-pyelftools
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_OPENSSL),y)
THINGINO_UBOOT_DEPENDENCIES += host-openssl
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_LZOP),y)
THINGINO_UBOOT_DEPENDENCIES += host-lzop
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_GNUTLS),y)
THINGINO_UBOOT_DEPENDENCIES += host-gnutls
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_UTIL_LINUX),y)
THINGINO_UBOOT_DEPENDENCIES += host-util-linux
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_NEEDS_XXD),y)
THINGINO_UBOOT_DEPENDENCIES += host-vim
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_BINMAN),y)
# https://source.denx.de/u-boot/u-boot/-/blob/v2024.04/tools/binman/binman.rst?plain=1#L377
# https://source.denx.de/u-boot/u-boot/-/blob/v2024.04/tools/buildman/requirements.txt
THINGINO_UBOOT_DEPENDENCIES += \
	host-python-jsonschema \
	host-python-pyyaml \
	host-python-yamllint
THINGINO_UBOOT_MAKE_OPTS += BINMAN_INDIRS=$(BINARIES_DIR)
endif

# prior to u-boot 2013.10 the license info was in COPYING. Copy it so
# legal-info finds it
define THINGINO_UBOOT_COPY_OLD_LICENSE_FILE
	if [ -f $(@D)/COPYING ]; then \
		$(INSTALL) -m 0644 -D $(@D)/COPYING $(@D)/Licenses/gpl-2.0.txt; \
	fi
endef

THINGINO_UBOOT_POST_EXTRACT_HOOKS += THINGINO_UBOOT_COPY_OLD_LICENSE_FILE
THINGINO_UBOOT_POST_RSYNC_HOOKS += THINGINO_UBOOT_COPY_OLD_LICENSE_FILE

# Older versions break on gcc 10+ because of redefined symbols
define THINGINO_UBOOT_DROP_YYLLOC
	$(Q)grep -Z -l -r -E '^YYLTYPE yylloc;$$' $(@D) \
	|xargs -0 -r $(SED) '/^YYLTYPE yylloc;$$/d'
endef
THINGINO_UBOOT_POST_PATCH_HOOKS += THINGINO_UBOOT_DROP_YYLLOC

# Copy sha1.h to tools directory for tools build
define THINGINO_UBOOT_COPY_SHA1_HEADER
	cp $(@D)/include/sha1.h $(@D)/tools/sha1.h
endef
THINGINO_UBOOT_POST_PATCH_HOOKS += THINGINO_UBOOT_COPY_SHA1_HEADER

ifneq ($(ARCH_XTENSA_OVERLAY_FILE),)
define THINGINO_UBOOT_XTENSA_OVERLAY_EXTRACT
	$(call arch-xtensa-overlay-extract,$(@D),u-boot)
endef
THINGINO_UBOOT_POST_EXTRACT_HOOKS += THINGINO_UBOOT_XTENSA_OVERLAY_EXTRACT
THINGINO_UBOOT_EXTRA_DOWNLOADS += $(ARCH_XTENSA_OVERLAY_URL)
endif

# Analogous code exists in linux/linux.mk. Basically, the generic
# package infrastructure handles downloading and applying remote
# patches. Local patches are handled depending on whether they are
# directories or files.
THINGINO_UBOOT_PATCHES = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_PATCH))
THINGINO_UBOOT_PATCH = $(filter ftp://% http://% https://%,$(THINGINO_UBOOT_PATCHES))

define THINGINO_UBOOT_APPLY_LOCAL_PATCHES
	for p in $(filter-out ftp://% http://% https://%,$(THINGINO_UBOOT_PATCHES)) ; do \
		if test -d $$p ; then \
			$(APPLY_PATCHES) $(@D) $$p \*.patch || exit 1 ; \
		else \
			$(APPLY_PATCHES) $(@D) `dirname $$p` `basename $$p` || exit 1; \
		fi \
	done
endef
THINGINO_UBOOT_POST_PATCH_HOOKS += THINGINO_UBOOT_APPLY_LOCAL_PATCHES

# Fixup inclusion of libfdt headers, which can fail in older u-boot versions
# when libfdt-devel is installed system-wide.
# The core change is equivalent to upstream commit
# e0d20dc1521e74b82dbd69be53a048847798a90a (first in v2018.03). However, the fixup
# is complicated by the fact that the underlying u-boot code changed multiple
# times in history:
# - The directory scripts/dtc/libfdt only exists since upstream commit
#   c0e032e0090d6541549b19cc47e06ccd1f302893 (first in v2017.11). For earlier
#   versions, create a dummy scripts/dtc/libfdt directory with symlinks for the
#   fdt-related files. This allows to use the same -I<path> option for both
#   cases.
# - The variable 'srctree' used to be called 'SRCTREE' before upstream commit
#   01286329b27b27eaeda045b469d41b1d9fce545a (first in v2014.04).
# - The original location for libfdt, 'lib/libfdt/', used to be simply
#   'libfdt' before upstream commit 0de71d507157c4bd4fddcd3a419140d2b986eed2
#   (first in v2010.06). Make the 'lib' part optional in the substitution to
#   handle this.
define THINGINO_UBOOT_FIXUP_LIBFDT_INCLUDE
	$(Q)if [ ! -d $(@D)/scripts/dtc/libfdt ]; then \
		mkdir -p $(@D)/scripts/dtc/libfdt; \
		cd $(@D)/scripts/dtc/libfdt; \
		ln -s ../../../include/fdt.h .; \
		ln -s ../../../include/libfdt*.h .; \
		ln -s ../../../lib/libfdt/libfdt_internal.h .; \
	fi
	$(Q)$(SED) \
		's%-I\ *\$$(srctree)/lib/libfdt%-I$$(srctree)/scripts/dtc/libfdt%; \
		s%-I\ *\$$(SRCTREE)\(/lib\)\?/libfdt%-I$$(SRCTREE)/scripts/dtc/libfdt%' \
		$(@D)/tools/Makefile
endef
THINGINO_UBOOT_POST_PATCH_HOOKS += THINGINO_UBOOT_FIXUP_LIBFDT_INCLUDE

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY),y)
define THINGINO_UBOOT_CONFIGURE_CMDS
	$(TARGET_CONFIGURE_OPTS) \
		$(THINGINO_UBOOT_MAKE) -C $(@D) $(THINGINO_UBOOT_MAKE_OPTS) \
		$(THINGINO_UBOOT_BOARD_NAME)_config
endef
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_KCONFIG),y)
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_DEFCONFIG),y)
THINGINO_UBOOT_KCONFIG_DEFCONFIG = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_BOARD_DEFCONFIG))_defconfig
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_CUSTOM_CONFIG),y)
THINGINO_UBOOT_KCONFIG_FILE = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CONFIG_FILE))
endif # BR2_PACKAGE_THINGINO_UBOOT_USE_DEFCONFIG

THINGINO_UBOOT_KCONFIG_FRAGMENT_FILES = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CONFIG_FRAGMENT_FILES))
THINGINO_UBOOT_KCONFIG_EDITORS = menuconfig xconfig gconfig nconfig

# THINGINO_UBOOT_MAKE_OPTS overrides HOSTCC / HOSTLDFLAGS to allow the build to
# find our host-openssl. However, this triggers a bug in the kconfig
# build script that causes it to build with /usr/include/ncurses.h
# (which is typically wchar) but link with
# $(HOST_DIR)/lib/libncurses.so (which is not).  We don't actually
# need any host-package for kconfig, so remove the HOSTCC/HOSTLDFLAGS
# override again. In addition, host-ccache is not ready at kconfig
# time, so use HOSTCC_NOCCACHE.
THINGINO_UBOOT_KCONFIG_OPTS = $(THINGINO_UBOOT_MAKE_OPTS) HOSTCC="$(HOSTCC_NOCCACHE)" HOSTLDFLAGS=""

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_DEFAULT_ENV_FILE_ENABLED),y)
UBOOT_DEFAULT_ENV_FILE = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_DEFAULT_ENV_FILE))
define THINGINO_UBOOT_KCONFIG_DEFAULT_ENV_FILE
	$(call KCONFIG_SET_OPT,CONFIG_USE_DEFAULT_ENV_FILE,y)
	$(call KCONFIG_SET_OPT,CONFIG_DEFAULT_ENV_FILE,"$(shell readlink -f $(THINGINO_UBOOT_DEFAULT_ENV_FILE))")
endef
endif
endif # BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY

THINGINO_UBOOT_CUSTOM_DTS_PATH = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_DTS_PATH))

define THINGINO_UBOOT_BUILD_CMDS
	$(if $(THINGINO_UBOOT_CUSTOM_DTS_PATH),
		cp -f $(THINGINO_UBOOT_CUSTOM_DTS_PATH) $(@D)/arch/$(THINGINO_UBOOT_ARCH)/dts/
	)
	$(TARGET_CONFIGURE_OPTS) \
		PKG_CONFIG="$(PKG_CONFIG_HOST_BINARY)" \
		PKG_CONFIG_SYSROOT_DIR="/" \
		PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
		PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 \
		PKG_CONFIG_LIBDIR="$(HOST_DIR)/lib/pkgconfig:$(HOST_DIR)/share/pkgconfig" \
		$(THINGINO_UBOOT_MAKE) -C $(@D) $(THINGINO_UBOOT_MAKE_OPTS) \
		$(THINGINO_UBOOT_MAKE_TARGET)
	$(if $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_SD),
		$(@D)/tools/mxsboot sd $(@D)/u-boot.sb $(@D)/u-boot.sd)
	$(if $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND),
		$(@D)/tools/mxsboot \
			-w $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND_PAGE_SIZE) \
			-o $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND_OOB_SIZE) \
			-e $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND_ERASE_SIZE) \
			nand $(@D)/u-boot.sb $(@D)/u-boot.nand)
endef

define THINGINO_UBOOT_BUILD_OMAP_IFT
	$(HOST_DIR)/bin/gpsign -f $(@D)/u-boot.bin \
		-c $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG))
endef

define THINGINO_UBOOT_INSTALL_IMAGES_CMDS
	$(foreach f,$(THINGINO_UBOOT_BINS), \
			cp -dpf $(@D)/$(f) $(BINARIES_DIR)/
	)
	$(if $(BR2_PACKAGE_THINGINO_UBOOT_FORMAT_NAND),
		cp -dpf $(@D)/u-boot.sb $(BINARIES_DIR))
	$(if $(BR2_PACKAGE_THINGINO_UBOOT_SPL),
		$(foreach f,$(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_SPL_NAME)), \
			cp -dpf $(@D)/$(f) $(BINARIES_DIR)/
		)
	)
endef

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP),y)

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PMUFW_EMBEDDEDSW),y)
THINGINO_UBOOT_DEPENDENCIES += xilinx-embeddedsw
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH = $(BINARIES_DIR)/pmufw.elf
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PMUFW_PREBUILT),y)
THINGINO_UBOOT_DEPENDENCIES += xilinx-prebuilt
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH = $(BINARIES_DIR)/pmufw.elf
else
THINGINO_UBOOT_ZYNQMP_PMUFW = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PMUFW))

ifneq ($(findstring ://,$(THINGINO_UBOOT_ZYNQMP_PMUFW)),)
THINGINO_UBOOT_EXTRA_DOWNLOADS += $(THINGINO_UBOOT_ZYNQMP_PMUFW)
BR_NO_CHECK_HASH_FOR += $(notdir $(THINGINO_UBOOT_ZYNQMP_PMUFW))
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH = $(THINGINO_UBOOT_DL_DIR)/$(notdir $(THINGINO_UBOOT_ZYNQMP_PMUFW))
else ifneq ($(THINGINO_UBOOT_ZYNQMP_PMUFW),)
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH = $(shell readlink -f $(THINGINO_UBOOT_ZYNQMP_PMUFW))
endif #ifneq ($(findstring ://,$(THINGINO_UBOOT_ZYNQMP_PMUFW)),)

endif #BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PMUFW_PREBUILT

ifeq ($(suffix $(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH)),.elf)
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH_FINAL = $(basename $(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH)).bin
# objcopy is arch-agnostic so we can use $(TARGET_OBJCOPY) in lack of a
# microblaze objcopy
define THINGINO_UBOOT_ZYNQMP_PMUFW_CONVERT
	$(TARGET_OBJCOPY) -O binary -I elf32-little $(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH) $(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH_FINAL)
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_UBOOT_ZYNQMP_PMUFW_CONVERT
else
THINGINO_UBOOT_ZYNQMP_PMUFW_PATH_FINAL = $(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH)
endif
define THINGINO_UBOOT_ZYNQMP_KCONFIG_PMUFW
	$(call KCONFIG_SET_OPT,CONFIG_PMUFW_INIT_FILE,"$(THINGINO_UBOOT_ZYNQMP_PMUFW_PATH_FINAL)")
endef

THINGINO_UBOOT_ZYNQMP_PM_CFG = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PM_CFG))
ifneq ($(THINGINO_UBOOT_ZYNQMP_PM_CFG),)
THINGINO_UBOOT_ZYNQMP_PM_CFG_BIN = $(THINGINO_UBOOT_DIR)/pm_cfg_obj.bin
define THINGINO_UBOOT_ZYNQMP_KCONFIG_PM_CFG
	$(call KCONFIG_SET_OPT,CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE,"$(THINGINO_UBOOT_ZYNQMP_PM_CFG_BIN)", \
		$(@D)/.config)
endef

define THINGINO_UBOOT_ZYNQMP_PM_CFG_CONVERT
	$(THINGINO_UBOOT_DIR)/tools/zynqmp_pm_cfg_obj_convert.py \
		"$(THINGINO_UBOOT_ZYNQMP_PM_CFG)" \
		"$(THINGINO_UBOOT_ZYNQMP_PM_CFG_BIN)"
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_UBOOT_ZYNQMP_PM_CFG_CONVERT
endif

THINGINO_UBOOT_ZYNQMP_PSU_INIT = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP_PSU_INIT_FILE))
THINGINO_UBOOT_ZYNQMP_PSU_INIT_PATH = $(shell readlink -f $(THINGINO_UBOOT_ZYNQMP_PSU_INIT))

ifneq ($(THINGINO_UBOOT_ZYNQMP_PSU_INIT),)
define THINGINO_UBOOT_ZYNQMP_KCONFIG_PSU_INIT
	$(call KCONFIG_SET_OPT,CONFIG_XILINX_PS_INIT_FILE,"$(THINGINO_UBOOT_ZYNQMP_PSU_INIT_PATH)")
endef
endif

endif # BR2_PACKAGE_THINGINO_UBOOT_ZYNQMP

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ZYNQ),y)
UBOOT_ZYNQ_PS7_INIT = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_ZYNQ_PS7_INIT_FILE))
UBOOT_ZYNQ_PS7_INIT_PATH = $(shell readlink -f $(THINGINO_UBOOT_ZYNQ_PS7_INIT))

ifneq ($(THINGINO_UBOOT_ZYNQ_PS7_INIT),)
define THINGINO_UBOOT_ZYNQ_KCONFIG_PS7_INIT
	$(call KCONFIG_SET_OPT,CONFIG_XILINX_PS_INIT_FILE,"$(THINGINO_UBOOT_ZYNQ_PS7_INIT_PATH)")
endef
endif
endif # BR2_PACKAGE_THINGINO_UBOOT_ZYNQ

define THINGINO_UBOOT_INSTALL_OMAP_IFT_IMAGE
	cp -dpf $(@D)/$(THINGINO_UBOOT_BIN_IFT) $(BINARIES_DIR)/
endef

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT),y)
ifeq ($(BR_BUILDING),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG)),)
$(error No gpsign config file. Check your BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG setting)
endif
ifeq ($(wildcard $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG))),)
$(error gpsign config file $(BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG) not found. Check your BR2_PACKAGE_THINGINO_UBOOT_OMAP_IFT_CONFIG setting)
endif
endif
THINGINO_UBOOT_DEPENDENCIES += host-omap-u-boot-utils
THINGINO_UBOOT_POST_BUILD_HOOKS += THINGINO_UBOOT_BUILD_OMAP_IFT
THINGINO_UBOOT_POST_INSTALL_IMAGES_HOOKS += THINGINO_UBOOT_INSTALL_OMAP_IFT_IMAGE
endif

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_ALTERA_SOCFPGA_IMAGE_CRC),y)
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_SPL),y)
THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_INPUT_IMAGES = $(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_SPL_NAME))
THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_HEADER_VERSION = 0
else
THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_INPUT_IMAGES = u-boot-dtb.bin
THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_HEADER_VERSION = 1
endif
define THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_IMAGE
	$(foreach f,$(THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_INPUT_IMAGES), \
		$(HOST_DIR)/bin/mkpimage \
			-v $(THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_HEADER_VERSION) \
			-o $(BINARIES_DIR)/$(notdir $(call qstrip,$(f))).crc \
			$(@D)/$(call qstrip,$(f))
	)
endef
THINGINO_UBOOT_DEPENDENCIES += host-mkpimage
THINGINO_UBOOT_POST_INSTALL_IMAGES_HOOKS += THINGINO_UBOOT_CRC_ALTERA_SOCFPGA_IMAGE
endif

define THINGINO_UBOOT_KCONFIG_FIXUP_CMDS
	$(THINGINO_UBOOT_ZYNQMP_KCONFIG_PMUFW)
	$(THINGINO_UBOOT_ZYNQMP_KCONFIG_PM_CFG)
	$(THINGINO_UBOOT_ZYNQMP_KCONFIG_PSU_INIT)
	$(THINGINO_UBOOT_ZYNQ_KCONFIG_PS7_INIT)
	$(THINGINO_UBOOT_KCONFIG_DEFAULT_ENV_FILE)
endef

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT)$(BR_BUILDING),yy)

#
# Set internal variables for flash controller
#
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_JZ_SFC),y)
	UBOOT_FLASH_CONTROLLER := "jz_sfc"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_SFC_NAND),y)
	UBOOT_FLASH_CONTROLLER := "sfc_nand"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_SFC0_NOR),y)
	UBOOT_FLASH_CONTROLLER := "sfc0_nor"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_SFC1_NOR),y)
	UBOOT_FLASH_CONTROLLER := "sfc1_nor"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_SFC0_NAND),y)
	UBOOT_FLASH_CONTROLLER := "sfc0_nand"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_SFC1_NAND),y)
	UBOOT_FLASH_CONTROLLER := "sfc1_nand"
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_CUSTOM),y)
	UBOOT_FLASH_CONTROLLER := $(BR2_PACKAGE_THINGINO_UBOOT_FLASH_CONTROLLER_CUSTOM_STRING)
endif

#
# Calculate reserved memory values and export to uenv.txt
#
define RMEM_SET_VALUE
	if [ -n "$(ISP_RMEM)" ]; then \
		if [ "$(SOC_FAMILY)" = "t20" -o "$(SOC_FAMILY)" = "t10" ]; then \
			osmem=$$(( $(SOC_RAM) - $(ISP_RMEM) - $(ISP_ISPMEM) )) && \
			ispmem_offset=$$(( ($(SOC_RAM) - $(ISP_RMEM) - $(ISP_ISPMEM)) * 0x100000 )) && \
			rmem_offset=$$(( ($(SOC_RAM) - $(ISP_RMEM)) * 0x100000 )) && \
			grep -q "^osmem=$${osmem}M@0x0" $(OUTPUT_DIR)/uenv.txt || echo "osmem=$${osmem}M@0x0" >> $(OUTPUT_DIR)/uenv.txt && \
			grep -q "^ispmem=$(ISP_ISPMEM)M@$$(printf '0x%x' $$ispmem_offset)" $(OUTPUT_DIR)/uenv.txt || echo "ispmem=$(ISP_ISPMEM)M@$$(printf '0x%x' $$ispmem_offset)" >> $(OUTPUT_DIR)/uenv.txt && \
			grep -q "^rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" $(OUTPUT_DIR)/uenv.txt || echo "rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" >> $(OUTPUT_DIR)/uenv.txt; \
		elif [ "$(SOC_FAMILY)" = "t40" -o "$(SOC_FAMILY)" = "t41" ]; then \
			osmem=$$(( $(SOC_RAM) - $(ISP_RMEM) - $(ISP_NMEM) )) && \
			rmem_offset=$$(( ($(SOC_RAM) - $(ISP_RMEM) - $(ISP_NMEM)) * 0x100000 )) && \
			nmem_offset=$$(( ($(SOC_RAM) - $(ISP_NMEM)) * 0x100000 )) && \
			grep -q "^osmem=$${osmem}M@0x0" $(OUTPUT_DIR)/uenv.txt || echo "osmem=$${osmem}M@0x0" >> $(OUTPUT_DIR)/uenv.txt && \
			grep -q "^rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" $(OUTPUT_DIR)/uenv.txt || echo "rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" >> $(OUTPUT_DIR)/uenv.txt && \
			grep -q "^nmem=$(ISP_NMEM)M@$$(printf '0x%x' $$nmem_offset)" $(OUTPUT_DIR)/uenv.txt || echo "nmem=$(ISP_NMEM)M@$$(printf '0x%x' $$nmem_offset)" >> $(OUTPUT_DIR)/uenv.txt; \
			echo HELLO $(ISP_NMEM); \
		else \
			osmem=$$(( $(SOC_RAM) - $(ISP_RMEM) )) && \
			rmem_offset=$$(( ($(SOC_RAM) - $(ISP_RMEM)) * 0x100000 )) && \
			grep -q "^osmem=$${osmem}M@0x0" $(OUTPUT_DIR)/uenv.txt || echo "osmem=$${osmem}M@0x0" >> $(OUTPUT_DIR)/uenv.txt && \
			grep -q "^rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" $(OUTPUT_DIR)/uenv.txt || echo "rmem=$(ISP_RMEM)M@$$(printf '0x%x' $$rmem_offset)" >> $(OUTPUT_DIR)/uenv.txt; \
		fi; \
	else \
		echo "No ISP_RMEM value set"; \
	fi
endef

THINGINO_UBOOT_PRE_BUILD_HOOKS += RMEM_SET_VALUE

#
# Replace appropriate values in uenv.txt with those from the device config
#

define THINGINO_GENERATE_UBOOT_ENV
	@env BR2_PACKAGE_THINGINO_UBOOT_ROOT='$(value BR2_PACKAGE_THINGINO_UBOOT_ROOT)' sh -c 'grep -q "^root=" $(OUTPUT_DIR)/uenv.txt || echo "root=$$BR2_PACKAGE_THINGINO_UBOOT_ROOT" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_ROOTFSTYPE='$(value BR2_PACKAGE_THINGINO_UBOOT_ROOTFSTYPE)' sh -c 'grep -q "^rootfstype=" $(OUTPUT_DIR)/uenv.txt || echo "rootfstype=$$BR2_PACKAGE_THINGINO_UBOOT_ROOTFSTYPE" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_INIT='$(value BR2_PACKAGE_THINGINO_UBOOT_INIT)' sh -c 'grep -q "^init=" $(OUTPUT_DIR)/uenv.txt || echo "init=$$BR2_PACKAGE_THINGINO_UBOOT_INIT" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_MTDPARTS='$(value BR2_PACKAGE_THINGINO_UBOOT_MTDPARTS)' sh -c 'grep -q "^mtdparts=" $(OUTPUT_DIR)/uenv.txt || echo "mtdparts=$$BR2_PACKAGE_THINGINO_UBOOT_MTDPARTS" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_BOOTARGS='$(value BR2_PACKAGE_THINGINO_UBOOT_BOOTARGS)' sh -c 'grep -q "^bootargs=" $(OUTPUT_DIR)/uenv.txt || echo "bootargs=$$BR2_PACKAGE_THINGINO_UBOOT_BOOTARGS" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD='$(value BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD)' sh -c 'grep -q "^bootcmd=" $(OUTPUT_DIR)/uenv.txt || echo "bootcmd=$$BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD='$(value BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD)' sh -c 'grep -q "^bootcmd=" $(OUTPUT_DIR)/uenv.txt || echo "bootcmd=$$BR2_PACKAGE_THINGINO_UBOOT_BOOTCMD" | sed "s/=\"/=/;s/\"$$//" >> $(OUTPUT_DIR)/uenv.txt'
	@env BR2_PACKAGE_THINGINO_UBOOT_SD_ENABLE='$(BR2_PACKAGE_THINGINO_UBOOT_SD_ENABLE)' sh -c 'if [ "$$BR2_PACKAGE_THINGINO_UBOOT_SD_ENABLE" = "y" ]; then grep -q "^disable_sd=" $(OUTPUT_DIR)/uenv.txt && sed -i "s/^disable_sd=.*/disable_sd=false/" $(OUTPUT_DIR)/uenv.txt || echo "disable_sd=false" >> $(OUTPUT_DIR)/uenv.txt; else grep -q "^disable_sd=" $(OUTPUT_DIR)/uenv.txt && sed -i "s/^disable_sd=.*/disable_sd=true/" $(OUTPUT_DIR)/uenv.txt || echo "disable_sd=true" >> $(OUTPUT_DIR)/uenv.txt; fi'
	@env BR2_PACKAGE_THINGINO_UBOOT_ETH_ENABLE='$(BR2_PACKAGE_THINGINO_UBOOT_ETH_ENABLE)' sh -c 'if [ "$$BR2_PACKAGE_THINGINO_UBOOT_ETH_ENABLE" = "y" ]; then grep -q "^disable_eth=" $(OUTPUT_DIR)/uenv.txt && sed -i "s/^disable_eth=.*/disable_eth=false/" $(OUTPUT_DIR)/uenv.txt || echo "disable_eth=false" >> $(OUTPUT_DIR)/uenv.txt; else grep -q "^disable_eth=" $(OUTPUT_DIR)/uenv.txt && sed -i "s/^disable_eth=.*/disable_eth=true/" $(OUTPUT_DIR)/uenv.txt || echo "disable_eth=true" >> $(OUTPUT_DIR)/uenv.txt; fi'
	@sed -i "s|\$$(UBOOT_FLASH_CONTROLLER)|$(UBOOT_FLASH_CONTROLLER)|g" $(OUTPUT_DIR)/uenv.txt
	@sh -c '[ "$(SOC_FAMILY)" = "t40" -o "$(SOC_FAMILY)" = "t41" ] && sed -i "s|\$$(UBOOT_NMEM)|nmem=$$\{nmem\} |g" $(OUTPUT_DIR)/uenv.txt || sed -i "s|\$$(UBOOT_NMEM)||g" $(OUTPUT_DIR)/uenv.txt'
	@sh -c '[ "$(SOC_FAMILY)" = "t20" -o "$(SOC_FAMILY)" = "t10" ] && sed -i "s|\$$(UBOOT_ISPMEM)| ispmem=$$\{ispmem\} |g" $(OUTPUT_DIR)/uenv.txt || sed -i "s|\$$(UBOOT_ISPMEM)| |g" $(OUTPUT_DIR)/uenv.txt'
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += THINGINO_GENERATE_UBOOT_ENV

#
# Patch uboot headers with env data for device if uenv.txt exists
#
define PATCH_DEV_ENV
	$(BR2_EXTERNAL)/scripts/uboot-device-env.sh $(OUTPUT_DIR)/uenv.txt \
		$(@D)/include/configs/isvp_common.h
endef
THINGINO_UBOOT_PRE_BUILD_HOOKS += PATCH_DEV_ENV

#
# Check U-Boot board name (for legacy) or the defconfig/custom config
# file options (for kconfig)
#
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY),y)
ifeq ($(THINGINO_UBOOT_BOARD_NAME),)
$(error No U-Boot board name set. Check your BR2_PACKAGE_THINGINO_UBOOT_BOARDNAME setting)
endif # THINGINO_UBOOT_BOARD_NAME
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_KCONFIG),y)
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_DEFCONFIG),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_BOARD_DEFCONFIG)),)
$(error No board defconfig name specified, check your BR2_PACKAGE_THINGINO_UBOOT_BOARD_DEFCONFIG setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_BOARD_DEFCONFIG
endif # BR2_PACKAGE_THINGINO_UBOOT_USE_DEFCONFIG
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_USE_CUSTOM_CONFIG),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CONFIG_FILE)),)
$(error No board configuration file specified, check your BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CONFIG_FILE setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CONFIG_FILE
endif # BR2_PACKAGE_THINGINO_UBOOT_USE_CUSTOM_CONFIG
endif # BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY

#
# Check custom version option
#
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_VERSION),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_VERSION_VALUE)),)
$(error No custom U-Boot version specified. Check your BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_VERSION_VALUE setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_VERSION_VALUE
endif # BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_VERSION

#
# Check custom tarball option
#
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL_LOCATION)),)
$(error No custom U-Boot tarball specified. Check your BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL_LOCATION setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL_LOCATION
endif # BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_TARBALL

#
# Check Git/Mercurial repo options
#
ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_GIT)$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_HG),y)
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_URL)),)
$(error No custom U-Boot repository URL specified. Check your BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_URL setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CUSTOM_REPO_URL
ifeq ($(call qstrip,$(BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_VERSION)),)
$(error No custom U-Boot repository version specified. Check your BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_REPO_VERSION setting)
endif # qstrip BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_CUSTOM_REPO_VERSION
endif # BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_GIT || BR2_PACKAGE_THINGINO_UBOOT_CUSTOM_HG

endif # BR2_PACKAGE_THINGINO_UBOOT && BR_BUILDING

ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY),y)
THINGINO_UBOOT_DEPENDENCIES += \
	$(BR2_BISON_HOST_DEPENDENCY) \
	$(BR2_FLEX_HOST_DEPENDENCY)
$(eval $(generic-package))
else ifeq ($(BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_KCONFIG),y)
THINGINO_UBOOT_MAKE_ENV = $(TARGET_MAKE_ENV)
# Starting with 2021.10, the kconfig in uboot calls the cross-compiler
# to check its capabilities. So we need the toolchain before we can
# call the configurators.
THINGINO_UBOOT_KCONFIG_DEPENDENCIES += \
	toolchain \
	$(BR2_MAKE_HOST_DEPENDENCY) \
	$(BR2_BISON_HOST_DEPENDENCY) \
	$(BR2_FLEX_HOST_DEPENDENCY)
$(eval $(kconfig-package))
endif # BR2_PACKAGE_THINGINO_UBOOT_BUILD_SYSTEM_LEGACY
